from .models import Book,Code
from .serializers import BookSerializer,CodeSerializer
from rest_framework import viewsets
import requests
from django.http import JsonResponse,HttpResponse
class BookSnippetList(viewsets.ModelViewSet):  # generics.GenericAPIView分页功能
    # mixins.ListModelMixin 调用list执行返回序列化内容
    lookup_field = 'name'

    queryset = Book.objects.all()
    serializer_class = BookSerializer


class CodeSnippetList(viewsets.ModelViewSet):  # generics.GenericAPIView分页功能
    # mixins.ListModelMixin 调用list执行返回序列化内容
    lookup_field = 'id'

    queryset = Code.objects.all()
    serializer_class = CodeSerializer







def Ref_url( request,id):
    print(id)
    res = requests.get('http://127.0.0.1:8000/api/maoyan/%s/' % id).json()
    appid=res.get('appid')
    path=res.get('path')
    print(res.get('appid'))
    print(res.get('path'))
    action = "get_wxa_code"
    __biz = "MzAxNDMyNzcyNg=="
    mid = "100000016"
    weapp_sn = "2a44c84173e6e11922d0dc0f631f2f7e"
    url = 'https://mp.weixin.qq.com/mp/appmsg_weapp'
    data = {
        'action': action,
        '__biz': __biz,
        'mid': mid,
        'weapp_sn': weapp_sn,
        'path': path,
        'weapp_appid': appid
    }
    response = requests.get(url, params=data)
    maoyan_url = response.json().get('url')

    res2=Code.objects.filter(id=id).update(png_url=maoyan_url)
    print(response.json().get('url'),11111)
    print(res2,22222)

    return JsonResponse({'png_url':maoyan_url})

def Kuaishou_url( request,id):
    print(id)
    res = requests.get('http://127.0.0.1:8000/api/kuaishou/%s/' % id).json()
    png_url = res.get('png_url')


    return JsonResponse({'png_url':png_url})
